/*
 * @Descripttion: 工具集
 * @version: 1.2
 * @LastEditors: sakuya
 * @LastEditTime: 2022年5月24日00:28:56
 */

const openAiTool = {};
openAiTool.data = {
	renderImagesGLM(val, elements, callback, checkStamp) {
		var stamp = 0;
		const contents = val
			.replace(/^data: /, "")
			?.split("data:")
			?.map((item) => item.replace(/\n\n$/, ""));
		// .slice(-1);
		// 开始循环集合
		contents.forEach((content) => {
			// 开始循环集合
			if (content.indexOf("[DONE]") != -1) {
				// 需要上传本地的回答对话
				console.log("this conent is DONE");

				callback("submitChatDone");
				return;
			}
			var data = JSON.parse(content);
			let checkStatus = checkStamp(data.created);
			if (!checkStatus) {
				return;
			}
			// 判断状态 是否是错误的状态
			let choices = data.choices[0];
			let sourceFiles = data.sourceFiles;

			let success = true;
			if (choices && choices.finish_reason == "error") {
				// 证明是 错误的状态，此时就追加到最后一个的元素里面吧
				el = elements.messages.at(elements.messages.length - 1);
				success = false;
			} else {
				var el = elements.messages.find(
					(map) => map.messageId == data.id
				);
				if (el == null) {
					el = {
						messageId: data.id,
						content: choices.message.content,
						role: "assistant",
						type: "answer",
					};
					elements.messages.push(el);
				}
			}
			let resultContent = choices.message.content;

			if (resultContent.indexOf("http") >= 0) {
				// 转换 mark down 语法
				// ![CSDN图标](https://csdnimg.cn/cdn/content-toolbar/csdn-logo_.png?v=20190924.1 "CSDN图标")
				// <img src=https://img2024.cnblogs.com/blog/2845014/202406/2845014-20240617165451537-517285446.jpg align="left" width=400 />
				// resultContent = `![](${resultContent} "") <br/><p></p>`;
				el.image = resultContent;
				//resultContent = `<img src=${resultContent}  width=400 height=400 /><br/><p></p>`;
			} else {
				el.content += resultContent;
			}

			if (sourceFiles) {
				el.sourceFiles = sourceFiles;
			}
			if (!success) {
				el.aiType = success ? "images" : "chat";
			}
			stamp = data.created;
		});
		if (stamp != 0) {
			callback("updateChatTimestamp", stamp);
		}
	},
	renderVideos(val, elements, callback, checkStamp) {
		var stamp = 0;
		const contents = val
			.replace(/^data: /, "")
			?.split("data:")
			?.map((item) => item.replace(/\n\n$/, ""));
		// .slice(-1);
		// 开始循环集合
		contents.forEach((content) => {
			// 开始循环集合
			if (content.indexOf("[DONE]") != -1) {
				// 需要上传本地的回答对话
				console.log("this conent is DONE");

				callback("submitChatDone");
				return;
			}
			var data = JSON.parse(content);
			let checkStatus = checkStamp(data.created);
			if (!checkStatus) {
				return;
			}
			// 判断状态 是否是错误的状态
			let choices = data.choices[0];
			let sourceFiles = data.sourceFiles;

			let success = true;
			if (choices && choices.finish_reason == "error") {
				// 证明是 错误的状态，此时就追加到最后一个的元素里面吧
				el = elements.messages.at(elements.messages.length - 1);
				success = false;
			} else {
				var el = elements.messages.find(
					(map) => map.messageId == data.id
				);
				if (el == null) {
					el = {
						messageId: data.id,
						content: choices.message.content,
						role: "assistant",
						type: "answer",
					};
					elements.messages.push(el);
				}
			}
			let resultContent = choices.message.content;
			if (resultContent.indexOf("url") >= 0) {
				// 证明有结果
				var videoData = JSON.parse(resultContent);
				el.videos = videoData;
			} else {
				el.content += resultContent;
			}

			if (sourceFiles) {
				el.sourceFiles = sourceFiles;
			}
			if (!success) {
				el.aiType = success ? "videos" : "chat";
			}
			stamp = data.created;
		});
		if (stamp != 0) {
			callback("updateChatTimestamp", stamp);
		}
	},
	/**
	 * 渲染对话主题
	 * @param {*} val
	 * @param {*} elements
	 * @param {*} callback
	 * @param {*} checkStamp
	 */
	renderTopic(val, element, callback) {
		if (!element.stamp) {
			element.stamp = 0;
		}
		if (!element.topicGenerate) {
			element.topicGenerate = "";
		}
		const contents = val
			.replace(/^data: /, "")
			?.split("data:")
			?.map((item) => item.replace(/\n\n$/, ""));
		var stamp = 0;
		// 开始循环集合
		contents.forEach((item) => {
			let content = item;
			if (content.indexOf("[DONE]") != -1) {
				// 需要上传本地的回答对话
				console.log("this conent is DONE");
				callback("submitChatDone");
				return;
			}
			var data = JSON.parse(content);
			let checkStatus = element.stamp < data.created;
			if (!checkStatus) {
				return;
			}
			// 判断状态 是否是错误的状态
			let choices = data.choices[0];
			if (choices && choices.finish_reason == "error") {
				// 证明是 错误的状态，此时就追加到最后一个的元素里面吧
				// 不需要处理
			} else {
				element.topicGenerate += choices.message.content;
			}

			stamp = data.created;
		});
		if (stamp != 0) {
			element.stamp = stamp;
		}
	},
	renderGLM(val, elements, callback, checkStamp) {
		const contents = val
			.replace(/^data: /, "")
			?.split("data:")
			?.map((item) => item.replace(/\n\n$/, ""));
		var stamp = 0;
		// 开始循环集合
		contents.forEach((item) => {
			let content = item;
			if (content.indexOf("[DONE]") != -1) {
				// 需要上传本地的回答对话
				console.log("this conent is DONE");

				callback("submitChatDone");
				return;
			}
			var data = JSON.parse(content);
			let checkStatus = checkStamp(data.created);
			if (!checkStatus) {
				return;
			}
			// 判断状态 是否是错误的状态
			let choices = data.choices[0];
			if (choices && choices.finish_reason == "error") {
				// 证明是 错误的状态，此时就追加到最后一个的元素里面吧
				el = elements.messages.at(elements.messages.length - 1);
			} else {
				var el = elements.messages.find(
					(map) => map.messageId == data.id
				);
				if (el == null) {
					el = {
						messageId: data.id,
						content: choices.message.content,
						role: "assistant",
						type: "answer",
					};
					elements.messages.push(el);
				}
			}
			el.content += choices.message.content;
			if (choices.web_search) {
				el.webSearch = choices.web_search;
			}

			if (data.sourceFiles) {
				el.sourceFiles = data.sourceFiles;
			}
			stamp = data.created;
		});
		if (stamp != 0) {
			callback("updateChatTimestamp", stamp);
		}
	},
	renderHunyuan(val, elements, callback, checkStamp) {
		const contents = val
			.replace(/^data: /, "")
			?.split("data:")
			?.map((item) => item.replace(/\n\n$/, ""));
		this.$openAiTool.data.render(val);
		var stamp = 0;
		contents.forEach((item) => {
			const content = item
				.replace(/^data: /, "")
				?.split("data:")
				?.map((item) => item.replace(/\n\n$/, ""))
				?.slice(-1);

			if (content[0].indexOf("[DONE]") != -1) {
				// 需要上传本地的回答对话

				this.sumitChatDoneMessage();
				callback("submitChatDone");
				return;
			}
			var data = JSON.parse(content[0]);
			let checkStatus = checkStamp(data.created);
			if (!checkStatus) {
				return;
			}
			// if (this.chatTimestamp >= data.created) {
			// 	return;
			// }
			var el = elements.messages.find((map) => map.messageId == data.id);
			if (el == null) {
				el = {
					messageId: data.id,
					content: data.choices[0].message.content,
					role: "assistant",
					type: "answer",
				};
				elements.messages.push(el);
				callback("updateActiveMessageCount");
				// this.activeMessageCount = elements.messages.length;
			} else {
				el.content += data.choices[0].message.content;
			}
			stamp = data.created;

			// this.chatTimestamp = data.created;
		});
		if (stamp != 0) {
			callback("updateChatTimestamp", stamp);
		}
	},
};
export default openAiTool;
